package p2p.peer;

import java.util.ArrayList;
import java.util.List;

import p2p.logic.connection.client.Client;

public class ClientManager {
	
	private List<Client> neighborClients;
	
	private Peer peer;
	
	public ClientManager(Peer peer){
		
		this.neighborClients = new ArrayList<Client>();
		
		this.peer = peer;
		
	}
	
	public List<Client> getClients(){
		return this.neighborClients;
	}
	
	public synchronized void addClient(Client client ){
		this.neighborClients.add(client);
	}
	
	public synchronized void addClients(List<Client> clientList ){
		for (Client c : clientList)
			this.neighborClients.add(c);
	}
	
	public synchronized String toString(){
		String res = "";
		for (Client c : neighborClients){
			res += c.toString() + ",";
		}
		if (res.length() > 1)
			return res.substring(0,res.length()-1);
		else
			return "No Neighbors";
	}
	
	public synchronized void removeClient(int index){
		this.neighborClients.remove(index);
	}
	
	public synchronized void removeClient(Client client){
		for (int i = 0; i < neighborClients.size(); i++){
			Client c = neighborClients.get(i);
			if (c.getIp().equals(client.getIp()) && c.getPort() == client.getPort()){
				this.neighborClients.remove(i);
			}
		}
	}

	public Peer getPeer() {
		return this.peer;
	}
	
	
	
}
